﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using bdxyit.com.RecruitmentAppSys.IDAL;
using bdxyit.com.RecruitmentAppSys.Models;
using System.Data.SqlClient;
using bdxyit.com.RecruitmentAppSys.DBUtility;
using System.Data;

/***
*机器名：BDXYIT-HUI
*项目名称：$projectname$
*命名空间：bdxyit.com.RecruitmentAppSys.SQLDAL
*文件名：LanguageLevelModels
*系统时间：3/25/2012 3:36:02 PM
*当前用户：huihui
*创建时间：2012
*
*修改时间：2012 3/25/2012 3:36:02 PM
*修改事件：
***/

namespace bdxyit.com.RecruitmentAppSys.SQLDAL
{
    public class LanguageLevelDAL : ILanguageLevelDAL
    {
        private const string SQL_SELECT_LANGUAGELEVEL = " select LevelID,LevelName,Type,SysID from LanguageLevels ";
        private const string SQL_SELECT_LANGUAGELEVEL_BY_LEVELID = " select  top 1 LevelID,LevelName,Type,SysID from LanguageLevels "
                                                                + "  where  LevelID=@LevelID";
        private const string SQL_DELETE_LANGUAGELEVEL_BY_LEVELID = " delete from LanguageLevels "
                                                                + "  where  LevelID=@LevelID";
        private const string SQL_UPDATE_LANGUAGELEVEL_BY_LEVELID = " update LanguageLevels set "
                                                                    + " LevelID=@LevelID,"
                                                                    + " LevelName=@LevelName,"
                                                                    + " Type=@Type,"
                                                                    + " SysID=@SysID"
                                                                    + "  where  LevelID=@LevelID";
        private const string SQL_INSERT_LANGUAGELEVEL = " insert into LanguageLevels("
                                                    + " LevelID,LevelName,Type,SysID)"
                                                    + "  values ("
                                                    + " @LevelID,@LevelName,@Type,@SysID)";
        private const string FIELD_LevelID = "@LevelID";
        private const string FIELD_LevelName = "@LevelName";
        private const string FIELD_Type = "@Type";
        private const string FIELD_SysID = "@SysID";
        public bool InsertLanguageLevel(LanguageLevel cmodel)
        {
            bool flag = false;
            SqlParameter[] LanguageLevelParms = GetLanguageLevelParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            LanguageLevelParms[0].Value = cmodel.LevelID;
            LanguageLevelParms[1].Value = cmodel.LevelName;
            LanguageLevelParms[2].Value = cmodel.Type;
            LanguageLevelParms[3].Value = cmodel.SysID;

            foreach (SqlParameter parm in LanguageLevelParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_INSERT_LANGUAGELEVEL;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool DeleteLanguageLevel(string LevelID)
        {
            bool flag = false;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_LevelID, SqlDbType.VarChar, 10);
            parm.Value = LevelID;
            SqlCommand cmd = new SqlCommand();
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_DELETE_LANGUAGELEVEL_BY_LEVELID;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public bool UpdateLanguageLevel(LanguageLevel cmodel)
        {
            bool flag = false;
            SqlParameter[] LanguageLevelParms = GetLanguageLevelParameters();
            SqlCommand cmd = new SqlCommand();
            //写添赋值语句
            LanguageLevelParms[0].Value = cmodel.LevelID;
            LanguageLevelParms[1].Value = cmodel.LevelName;
            LanguageLevelParms[2].Value = cmodel.Type;
            LanguageLevelParms[3].Value = cmodel.SysID;

            foreach (SqlParameter parm in LanguageLevelParms)
                cmd.Parameters.Add(parm);
            using (SqlConnection conn = new SqlConnection(SqlHelper.ConnectionStringLocal))
            {
                conn.Open();
                cmd.Connection = conn;
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = SQL_UPDATE_LANGUAGELEVEL_BY_LEVELID;
                cmd.ExecuteNonQuery();
                cmd.Parameters.Clear();
                flag = true;
            }
            return flag;
        }
        public LanguageLevel SelectLanguageLevelByLevelID(string LevelID)
        {
            LanguageLevel cmodel = null;
            //创建参数
            SqlParameter parm = new SqlParameter(FIELD_LevelID, SqlDbType.VarChar, 10);
            parm.Value = LevelID;
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_LANGUAGELEVEL_BY_LEVELID, parm))
            {
                if (dr.Read())
                {
                    cmodel = new LanguageLevel();
                    if (dr.HasRows == true)
                    {
                        if (dr["LevelID"] != null && dr["LevelID"].ToString() != "")
                        {
                            cmodel.LevelID = dr["LevelID"].ToString();
                        }
                        if (dr["LevelName"] != null && dr["LevelName"].ToString() != "")
                        {
                            cmodel.LevelName = dr["LevelName"].ToString();
                        }
                        if (dr["Type"] != null && dr["Type"].ToString() != "")
                        {
                            cmodel.Type = dr["Type"].ToString();
                        }
                        if (dr["SysID"] != null && dr["SysID"].ToString() != "")
                        {
                            cmodel.SysID = dr["SysID"].ToString();
                        }
                    }
                }
            }
            return cmodel;
        }
        public List<LanguageLevel> SelectLanguageLevel()
        {
            List<LanguageLevel> selectlist = new List<LanguageLevel>();
            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.ConnectionStringLocal, CommandType.Text, SQL_SELECT_LANGUAGELEVEL))
            {
                while (dr.Read())
                {
                    LanguageLevel cmodel = new LanguageLevel();
                    //写添赋值语句
                    if (dr["LevelID"] != null && dr["LevelID"].ToString() != "")
                    {
                        cmodel.LevelID = dr["LevelID"].ToString();
                    }
                    if (dr["LevelName"] != null && dr["LevelName"].ToString() != "")
                    {
                        cmodel.LevelName = dr["LevelName"].ToString();
                    }
                    if (dr["Type"] != null && dr["Type"].ToString() != "")
                    {
                        cmodel.Type = dr["Type"].ToString();
                    }
                    if (dr["SysID"] != null && dr["SysID"].ToString() != "")
                    {
                        cmodel.SysID = dr["SysID"].ToString();
                    }

                    selectlist.Add(cmodel);
                }
            }
            return selectlist;
        }
        private SqlParameter[] GetLanguageLevelParameters()
        {
            SqlParameter[] parms = new SqlParameter[] {
                    new SqlParameter(FIELD_LevelID, SqlDbType.VarChar,10),
					new SqlParameter(FIELD_LevelName, SqlDbType.VarChar,50),
					new SqlParameter(FIELD_Type, SqlDbType.VarChar,20),
					new SqlParameter(FIELD_SysID, SqlDbType.VarChar,10)};
            return parms;
        }

    }
}